package com.edgclub.edg.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.edgclub.edg.Constant;
import com.edgclub.edg.GuZhiApplication;
import com.edgclub.edg.R;
import com.edgclub.edg.huanxin.DemoHelper;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.CommAppPrefUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.zhzhinn.activity.ZZLoginActivity;
import com.zhzhinn.activity.ZZMainActivity;
import com.zhzhinn.activity.ZZRegisterSelectIconActivity;

import java.io.File;

public class GzSplashActivity extends GzBaseActivity implements View.OnClickListener {

	private CommAppPrefUtils appPrefUtils;

	private ImageView ll_shanp;


	private LinearLayout rootlayout;

	GlideUtils glideUtils;
	private boolean onclick;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_gz_splash);
		ll_shanp = (ImageView) findViewById(R.id.shanp);
		ll_shanp.setOnClickListener(this);
		rootlayout = (LinearLayout) findViewById(R.id.rootlayout);
		glideUtils=new GlideUtils();
		appPrefUtils = new CommAppPrefUtils(this);

		try {
			if((new File(Constant.bgDirImage)).exists()&&null!= BitmapFactory.decodeFile(Constant.bgDirImage)){
				glideUtils.Glidde_loadSplashImage(this,Constant.bgDirImage,ll_shanp);
			}else{
				ll_shanp.setImageResource(R.drawable.shanp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		mHandler.sendEmptyMessageDelayed(1,2000);

	}

	private Handler mHandler=new Handler() {
		@Override
		public void handleMessage (Message msg){
			switch (msg.what){
				case 1:
					if(!onclick) {
						if(appPrefUtils.isGuide()){
							startActivity(new Intent(GzSplashActivity.this, GzGuideActivity.class));
						}else {
							if (DemoHelper.getInstance().isLoggedIn()) {
								GuZhiApplication.getInstance().loginAgain();

								if ("".equals(new UserPrefUtils(GzSplashActivity.this).getUser().getLogo())) {
									Intent intent = new Intent(GzSplashActivity.this,
											ZZRegisterSelectIconActivity.class);
									startActivity(intent);
								} else {
									GuZhiApplication.isFirstMianActivity = true;
									Intent intent = new Intent(GzSplashActivity.this,
											GzMainActivity.class);
									startActivity(intent);
								}
							}else{
								startActivity(new Intent(GzSplashActivity.this, ZZLoginActivity.class));
							}
						}
						finish();
					}
					break;
			}
		}
	};


	private WindowManager wm;
	private WindowManager.LayoutParams params;
	private TextView floatView;

	private void createFloatView() {
		floatView = new TextView(this);
		floatView.setText(".");
		floatView.setTextColor(this.getResources().getColor(
				android.R.color.transparent));
		wm = (WindowManager) getApplicationContext().getSystemService(
				Context.WINDOW_SERVICE);
		params = new WindowManager.LayoutParams();

		params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
		params.format = PixelFormat.RGBA_8888;
		params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
		params.width = 1;
		params.height = 1;
		wm.addView(floatView, params);
	}



	@Override
	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.shanp){
			String linkUrl = appPrefUtils.getLinkUrl();
			if(!"".equals(linkUrl)) {
				onclick=true;
				mHandler.removeMessages(0);
				Intent intent = new Intent(GzSplashActivity.this, GzWebViewActivity.class);
				intent.putExtra("url", linkUrl);
				intent.putExtra("startlink", true);
				GzSplashActivity.this.startActivity(intent);
				GzSplashActivity.this.finish();
			}
		}
	}
}
